import MyUpload from '@/components/MyUpload';
import { editNavigation } from '@/services/advertising';
import { getResult } from '@/services/sys/attribute';
import { findByDataCategoryListPageSelect } from '@/services/sys/MenuService';
import {
  ModalForm,
  ProForm,
  ProFormGroup,
  ProFormItem,
  ProFormSelect,
  ProFormText,
} from '@ant-design/pro-components';
import { Button, message } from 'antd';
import React, { useEffect } from 'react';
interface Props {
  row: any;
  success?: () => void;
}

/*const columnDialogs = [{
  title: "编号",
  key: "id",
  dataIndex: "id",
  search: false,
  formItemProps: {
    hidden: true
  }
}, {
  title: "编号",
  key: "imageId",
  dataIndex: "imageId",
  search: false,
  formItemProps: {
    hidden: true
  }
}, {
  title: "基本信息", valueType: "group", columns: [{
    title: "图片",
    key: "image",
    dataIndex: "image",
    valueType: "image",
    width: 'md', renderFormItem: (item: any, f: any, form: any) => {
      const image = form.getFieldValue ( 'image' );
      const id = form.getFieldValue ( 'id' );
      return (<> <MyUpload
          type={5}
          url={image}
          id={id}
          onChange={(event) => {
            console.log ( "MyUpload", event )
            formRef.current?.setFieldsValue ( {imageId: event} );
          }}
        ></MyUpload></>
      )

    }
  }, {
    title: "汉语名称",
    key: "name",
    dataIndex: "name"
    ,
    width: 'md'
  }, {
    title: "维语名称",
    key: "nameUg",
    dataIndex: "nameUg"
    ,
    width: 'md'
  }, {
    title: "目标类型",
    key: "targetType",
    dataIndex: "type",
    valueType: "select",

    request: async () => {
      return await getResult ( 'advertisingTargetType', 1 );
    },
    width: 'md'
  },  {
    title: '地址',
    dataIndex: 'path',
    key: 'path',
    width: 'sm',
    valueType: "select",
  //  request: request1
  }, {
    title: "APPID",
    key: "appid",
    dataIndex: "appid",
    width: 'md'
  }, {
    title: "备注",
    key: "remark",
    dataIndex: "remark",
    valueType: "textarea",
    width: 'md'
  }]
}]*/
const request1 = async () => {
  return new Promise<API.SelectItem[]>((resolve, reject) => {
    findByDataCategoryListPageSelect()
      .then((res) => {
        let selectItems = res;
        // @ts-ignore
        selectItems.unshift({ name: '下一单', label: '下一单', value: '' });
        resolve(selectItems);
      })
      .catch((e) => {
        console.log(e);
        reject(e);
      });
  });
};
const NavEdit: React.FC<Props> = (props) => {
  const { row, success } = props;

  const [form] = ProForm.useForm();
  useEffect(() => {
    form.setFieldsValue(row);
  }, [row]);

  // @ts-ignore
  return (
    <ModalForm<API.MenuInfo>
      initialValues={row}
      title="菜单管理"
      form={form}
      trigger={<Button type="primary">新增/编辑</Button>}
      onFinish={(values) => {
        // values.id = menuInfo.id;
        console.log(values);
        return new Promise<Boolean>((resolve, reject) => {
          editNavigation(values)
            .then(() => {
              message.success('操作成功');

              //     refresh ();
              resolve(true);
              if (success) {
                success();
              }
              form.resetFields();
            })
            .catch(() => {
              message.success('操作失败');
              reject(false);
            });
        });
      }}
    >
      <ProFormText hidden={true} width="md" name="id" />
      <ProFormText hidden={true} width="md" name="imageId" />
      <ProFormGroup>
        <ProFormItem label={'图标'} name={'image'}>
          <MyUpload
            type={5}
            onChange={(imageId) => {
              form.setFieldValue('imageId', imageId);
            }}
          />
        </ProFormItem>
        <ProFormText width={'md'} label={'名称'} name={'name'}></ProFormText>
      </ProFormGroup>
      <ProFormGroup>
        <ProFormText
          width={'md'}
          label={'维语名称'}
          name={'nameUg'}
        ></ProFormText>
        <ProFormSelect
          request={request1}
          width={'md'}
          label={'地址'}
          name={'path'}
        ></ProFormSelect>
      </ProFormGroup>
      <ProFormGroup>
        <ProFormText width={'md'} label={'备注'} name={'remark'}></ProFormText>
        <ProFormText width={'md'} label={'APPID'} name={'appid'}></ProFormText>
      </ProFormGroup>
      <ProFormGroup>
        <ProFormSelect
          request={() => {
            return getResult('advertisingTargetType', 1);
          }}
          width={'md'}
          label={'目标类型'}
          name={'targetType'}
        ></ProFormSelect>
        <ProFormText width={'md'} label={'排序'} name={'sort'}></ProFormText>
      </ProFormGroup>
    </ModalForm>
  );
};
export default NavEdit;
